﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using Eacademy.Domain.Abstractions;
using Eacademy.Domain.Models;
using Eacademy.Library.Actions;
using Eacademy.Library.Membership;

namespace Eacademy.Areas.Student.Controllers
{
    [CustomAuthorize]
    [ProfileControllerFilter]
    public class LessonsController : Controller
    {
        private const int DefaultPageSize = 10;
        private ISubjectRepository _subjectRepository;
        private IMentorRepository _mentorRepository;
        private ILessonRepository _lessonRepository;

        
        private string SavePath
        {
            get
            {
                return String.Format(@"{0}", Server.MapPath(AppSettings.FileDirectory));
            }
        }


        public LessonsController(ISubjectRepository subjectRepository, IMentorRepository mentorRepository, ILessonRepository lessonRepository)
        {
            _subjectRepository = subjectRepository;
            _mentorRepository = mentorRepository;
            _lessonRepository = lessonRepository;
        }

        CustomMembershipProvider provider = (CustomMembershipProvider)Membership.Provider;

        //
        // GET: /Mentor/Lessons/

        public ActionResult Index(int id, int? page,int mentoruserid)
        {
            int currentPageIndex = page.HasValue ? (page.Value <= 0 ? 0 : page.Value - 1) : 0;
            AdminLessonListViewModel adminLessonListViewModel = new AdminLessonListViewModel();
            int totalcount = 0;
            List<AdminLessonsViewModel> lessonList = _lessonRepository.GetLessonsBySubjectId(id, mentoruserid, currentPageIndex + 1, DefaultPageSize, out totalcount);
            adminLessonListViewModel.LessonsList = lessonList.ToPagedList(currentPageIndex, DefaultPageSize, totalcount);
            adminLessonListViewModel.Subject = _subjectRepository.GetSubjectBySubjectId(id);
            return View(adminLessonListViewModel);
        }

        public ActionResult Details(int id)
        {
            AdminLessonsViewModel adminLessonsViewModel = _lessonRepository.GetLessonByLessonId(id);
            adminLessonsViewModel.FileList = new List<string>(adminLessonsViewModel.Files.Split(','));
            return View(adminLessonsViewModel);
        }

        public ActionResult Download(string name,int id)
        {
            string filename = String.Format("{0}_-{1}",id,name);
            return new DownloadResult { VirtualPath = AppSettings.FileDirectory + filename, FileDownloadName = name };
        }
    }
}
